1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

Mark Meyer | Parametric surfaces | basic sphere

markmeyer_parametric9b

size(550, 550)
background(0)
nofill()
stroke(0.6, 0.6, 0.7, 0.35)
strokewidth(0.5)
 
translate(WIDTH/2, HEIGHT/2)
 
# n defines how many vertices we find: the number will be n^2.
# Large values of n can take a while to render!
n = 27
 
# Define parametric equations for each axis.
# Although we define an z coordinate, 
# it is only used in transformations and shading; 
# it is not used in the drawing 
# (although it could be if you wanted to add perspective).
r = 240
def x(u, v): return r * sin(u) * cos(v)
def y(u, v): return r * sin(u) * sin(v)
def z(u, v): return r * cos(u)
 
def fit_to_domain(u, v):
    u = 1.0 * u / n * pi
    v = 1.0 * v / n * pi*2
    return u, v
 
from Numeric import *
def matrix(u, v, index):
    """
    Numeric's fromfunction() gives us integers for each index in the array.
    With matrix() applied, fromfunction() returns an array with all 
    the vertices we want. 
    """
    u, v = fit_to_domain(u, v)
    return where(index==0, x(u, v),
                 where(index==1, y(u,v),
                    where(index==2,  z(u,v), 1)
                    )
                 )
 
def project(rows):
    """
    Go through the array and draw rectangles.
    There is probably a more efficent way to do this.
    """
    for i in range(len(rows)-1):
        for j in range(len(rows[i])-1):
            beginpath(
                rows[i][j][0], 
                rows[i][j][1]
            ) 
            lineto(
                rows[i+1][j][0], 
                rows[i+1][j][1]
            )
            lineto(
                rows[i+1][j+1][0], 
                rows[i+1][j+1][1]
            )
            lineto(
                rows[i][j+1][0], 
                rows[i][j+1][1]
            )
            endpath()
            
sphere = fromfunction(matrix, (n+1, n+1, 3))
project(sphere)